<?php

class Controller_Admin_MailList extends Controller_Admin_Template
{
	public function before()
	{
		parent::before();
		$this->model = Model::factory('mailList');
	}

	public function actionIndex()
	{
		$this->addScript('/scripts/jquery/jquery.js');
		$this->addScript('/scripts/jquery/tablefilter/table.filter.js');
		$this->template->title = 'Subscribers management';
		$this->template->content = new View('admin/subscribers/list');
		$this->template->content->subscribers = $this->model->getSubscribers();
	}

	public function actionCompose()
	{
		$this->addScript('/scripts/jquery/jquery.js');
		$this->template->title = 'Compose a message';
		$this->template->content = new View('admin/subscribers/compose');
		$subscribers = $this->template->content->subscribers = $this->model->getSubscribers(true);
		$form = new Form('message');
		$this->addScript('/scripts/jquery/jquery-ui.js');
		$this->addScript('/scripts/elrte/elrte.min.js');
		$this->addScript('/scripts/plugin/editor.js');
		$this->addLink(array(
			'href' => '/styles/jquery-ui/ui-custom/jquery-ui.css',
			'rel'  => 'stylesheet',
			'type' => 'text/css',
		));
		$this->addLink(array(
			'href' => '/styles/elrte/elrte.full.css',
			'rel'  => 'stylesheet',
			'type' => 'text/css',
		));
		$form->hidden('recepients');
		$form->labelWidth = '0px';
		$form->text('subject', 'Subject')->attribute('size', 40)->rule('required')->bunched();
		$form->textarea('message', 'Message')->
			attribute('cols', 100)->
			attribute('rows', 15)->
			attribute('class', 'elrte')->
			rule('required')->
			callback('clearBr')->
			bunched();
		function clearBr($field) {
			if ($field->value == '<br>') {
				$field->errors[] = 'This field is required';
			}
		}
		$form->checkbox('copy', '', 'Send copy to my email');
		$this->template->content->form = $form;
		if ('POST' == Request::$method) {
			if ($form->validate()) {
				$values = $form->getValues();
				$message['html'] = $values['message'];
				$subj = $values['subject'];
				$from = $this->settings['title'] . ' <' . $this->settings['email'] . '>';
				$recipients = explode('|', $values['recepients']);
				foreach ($subscribers as $subscriber) {
					if (in_array($subscriber['id'], $recipients)) {
						$to = $subscriber['email'];
						Mail::sendMail($to, $subj, $message, $from);
					}
				}
				if ($values['copy']) {
					Mail::sendMail($from, $subj, $message, $from);
				}
				$this->response->redirect(Request::$controller . 'success/');
			}
		}
	}

	public function actionSuccess()
	{
		$this->template->title = 'Message was sent';
		$this->template->content = new View('admin/subscribers/message');
	}

	public function actionAdd()
	{
		$this->template->title = 'Add subscriber';
		$this->actionEdit();
	}

	public function actionEdit($id = null)
	{
		if ($id) {
			$this->template->title = 'Edit subscriber profile';
			$form = new Form('subscriber');
		} else {
			$form = new Form('subscriber');
		}
		$form->attributes['autocomplete'] = 'off';
		$form->labelWidth = '150px';
		$form->text('name', 'Full name')->attribute('size', 32)
			->rule('maxLength', 64)->rule('required');
		$form->text('email', 'E-mail')->attribute('size', 32)
			->rule('maxLength', 64)->rule('email')->rule('required')
			->callback('emailCheck', array('id' => $id));
		function emailCheck($field, $args) {
			extract($args);
			Model_Table::instance('mailList')->keys['string'] = 'email';
			if (Model_Table::instance('mailList')->exists($field->value, $id)) {
				$field->error(Text::DUPLICATE_KEY);
			}
		}
		$form->checkbox('confirmed', '', 'Confirmed e-mail', 1);
		$form->checkbox('emailSubscriber', '', 'Send subscriber confirmation email');
		$form->submit('submit', 'Add');
		if ($id) {
			$form->elements['submit']->value = 'Save';
			$subscriber = $this->model->getSubscriber($id);
			$form->loadValues($subscriber);
		}
		if ('POST' == Request::$method) {
			if ($form->validate()) {
				if ($id) {
					$values = $form->getModified();
					if (isset($values['emailSubscriber'])) {
						if ($values['emailSubscriber'] == 1) {
							MailList::getInstance()->sendConfirmation($id);
						}
						unset($values['emailSubscriber']);
					}
					Model_Table::instance('mailList')->update($values, $id);
				} else {
					$values = $form->getValues();
					$confirmation = false;
					if (isset($values['emailSubscriber'])) {
						if ($values['emailSubscriber'] == 1) {
							$confirmation = true;
						}
						unset($values['emailSubscriber']);
					}
					MailList::getInstance()->subscribe($values, $confirmation);
				}
				$this->response->redirect(Request::$controller);
			}
		}
		$this->template->content = new View('admin/subscribers/form', array('form' => $form));
	}

	public function actionRemove($email)
	{
		Model_Table::instance('mailList')->primaryKey = 'email';
		Model_Table::instance('mailList')->keys['string'] = 'email';
		Model_Table::instance('mailList')->delete($email);
		$this->response->redirect(Request::$controller);
	}
}